/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

/**
 *
 * @author Joao
 */
import banco.ConexaoBD;
import banco.UsuarioDAO;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "professorConverter")
public class ProfessorConverter implements Converter {

    public static List<String> professor;
     private String[][] vetorProfessores;

    UsuarioDAO banco = new UsuarioDAO(ConexaoBD.getConexaoBD());

    public ProfessorConverter() throws SQLException{
       
        vetorProfessores = banco.getProfessorsNames();
        
        professor = new ArrayList<String>();
        
        for(int i = 1; i < vetorProfessores.length; i++){
            professor.add(vetorProfessores[i][0] + " (" + vetorProfessores[i][1] + ")");
        }
    }

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {

        if (submittedValue.trim().equals("")) {
            return null;
        } else {
            System.out.println("VAlor" + submittedValue);
            for(int a = 0; a < professor.size(); a++){
                    
                if (professor.get(a).equals(submittedValue)) {
                    System.out.println("Entrou! " + a);
                        return professor.get(a);
                    }
        }
        }
        return null;
    }


    @Override
    public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
        if (value == null || value.equals("")) {
            return "";
        } else {
            System.out.println("String (value) " + (String) value);
            return ((String) value);
        }
    }
}